package com.tanhua.dubbo.api;

import com.tanhua.manage.domain.mongo.RecommendUser;
import com.tanhua.manage.domain.mongo.UserLike;
import com.tanhua.manage.domain.vo.PageResult;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserLikeApiImpl implements UserLikeApi {

    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private FriendApi friendApi;


    @Override
    public Long fanCount(Long userId) {
        Query query = new Query(Criteria.where("likeUserId").is(userId));
        return mongoTemplate.count(query, UserLike.class);
    }

    @Override
    public Long loveCount(Long userId) {
        Query query = new Query(Criteria.where("userId").is(userId));
        return mongoTemplate.count(query, UserLike.class);
    }

    @Override
    public PageResult findPageOneSideLike(Long userId, Long page, Long pageSize) {
        Query query = new Query(Criteria.where("userId").is(userId));
        long total = mongoTemplate.count(query, UserLike.class);
        List<RecommendUser> userList= new ArrayList<>();
        if (total>0) {
            query.skip((page-1)*pageSize).limit(pageSize.intValue());
            query.with(Sort.by(Sort.Order.desc("created")));
            List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);

            List<Long> likeUserIds = userLikes.stream().map(UserLike::getLikeUserId).collect(Collectors.toList());
            Query userQuery = new Query(Criteria.where("toUserId").is(userId).and("userId").in(likeUserIds));
            userList = mongoTemplate.find(userQuery, RecommendUser.class);

        }
        return PageResult.pageResult(page,pageSize,userList,total);
    }

    @Override
    public PageResult findPageFens(Long userId, Long page, Long pageSize) {
        Query query = new Query(Criteria.where("likeUserId").is(userId));
        long total = mongoTemplate.count(query, UserLike.class);
        List<RecommendUser> userList= new ArrayList<>();
        if (total>0) {
            query.skip((page-1)*pageSize).limit(pageSize.intValue());
            query.with(Sort.by(Sort.Order.desc("created")));
            List<UserLike> fansList = mongoTemplate.find(query, UserLike.class);

                List<Long> fansIds = fansList.stream().map(UserLike::getLikeUserId).collect(Collectors.toList());
                Query userQuery = new Query(Criteria.where("toUserId").is(userId).and("userId").in(fansIds));
                userList = mongoTemplate.find(userQuery, RecommendUser.class);

        }
        return PageResult.pageResult(page,pageSize,userList,total);
    }

    @Override
    public Boolean fansLike(Long userId, Long fansId) {
        //判断对方是否喜欢我
        Query query = new Query(Criteria.where("likeUserId").is(userId).and("userId").is(fansId));
        //如果喜欢
        if(mongoTemplate.exists(query, UserLike.class)){
            mongoTemplate.remove(query,UserLike.class);
            friendApi.makeFriend(userId,fansId);
            return true;
        }else {
            //如果不喜欢我了，则要添加我喜欢她记录
            UserLike userLike = new UserLike();
            userLike.setUserId(userId);
            userLike.setLikeUserId(fansId);
            userLike.setCreated(System.currentTimeMillis());
            mongoTemplate.insert(userLike);
        }
        return null;
    }
}
